<?php

# php cos.php

include __DIR__.'/vendor/autoload.php';

$url = 'http://api.weixin.qq.com/_/cos/getauth'; //tcb 开放接口服务 才能调用此接口
// 获取临时凭证

if (getenv('CBR_ENV_ID') != '') //tcb环境中才能获取临时appid key
{
    $tempKey = getJson($url);
    var_dump($tempKey);
    //{"TmpSecretId":"AKIDdylk4jikn-dUTdNuVOFWMrAE-K399KuR4IueRWlotS3_7VrS-r9BsjxJhqtm1Hm1","TmpSecretKey":"vOxXz1n6GHLLLgnxjLp3FzbPmtMpfMl1OKBAYGW/rM0=","ExpiredTime":1648809750,"Token":"ZG57naPqVqKPW3W1ybc9huCUSbcB4cta8bbbb1aff1bf2fb6adacc0326f55f334s7m-o8ENv_uVVxbH99tRo0oJErbEI5wxm_OMtuHH_YYd7GnIWZR4BPuJEUpyUT_JWAWZYwW9L0-jzVtheHe0SqeCy0DPYRm1hR-E0HoXZIGuE8LU_HZJOH7yp0pmb2S5y-QCYlAaDs1fk3H1jbB-B__sY-jGPqz7leed8A5OwHO1mfbO36UOz-rbTHJdcWOvH4ygip7t4Zzp53XQPmLshX97yMpm8TR9_WB0lRk86dndLPx-Y9RbMT_vBQERq0zQS2xsS_pE-NnEkOo-VHhSHTd_OShU6rhNGxnh4aWHuYeXots8_kK2kSPq1HrTgaD4-ZIREwqyeh9-UBYUy5mNCimW1RKKldOScSEvhRssicTqwVZ30wIDIG-9c5kYybGyb7Owu0Pnjg1H4Dq01RHWi7P6d5F7Kthcnf8I6MG4YPGQ8yuw_JdWNL1luywJoSuSsSMidAn130XOvi8aWtBZbA"}

}


// 创建bucket
appendObject(); //上传文件 读取文件


function getJson($url)
{
    $client = new \GuzzleHttp\Client();
    $response = $client->get($url);
    return json_decode($response->getBody()->getContents());
}

function appendObject()
{
    $bucket = '7765-weiqing-0gk5cx1z2402c6fc-1310251736'; //云托管bucket
    //{"TmpSecretId":"AKIDdylk4jikn-dUTdNuVOFWMrAE-K399KuR4IueRWlotS3_7VrS-r9BsjxJhqtm1Hm1","TmpSecretKey":"vOxXz1n6GHLLLgnxjLp3FzbPmtMpfMl1OKBAYGW/rM0=","ExpiredTime":1648809750,"Token":"ZG57naPqVqKPW3W1ybc9huCUSbcB4cta8bbbb1aff1bf2fb6adacc0326f55f334s7m-o8ENv_uVVxbH99tRo0oJErbEI5wxm_OMtuHH_YYd7GnIWZR4BPuJEUpyUT_JWAWZYwW9L0-jzVtheHe0SqeCy0DPYRm1hR-E0HoXZIGuE8LU_HZJOH7yp0pmb2S5y-QCYlAaDs1fk3H1jbB-B__sY-jGPqz7leed8A5OwHO1mfbO36UOz-rbTHJdcWOvH4ygip7t4Zzp53XQPmLshX97yMpm8TR9_WB0lRk86dndLPx-Y9RbMT_vBQERq0zQS2xsS_pE-NnEkOo-VHhSHTd_OShU6rhNGxnh4aWHuYeXots8_kK2kSPq1HrTgaD4-ZIREwqyeh9-UBYUy5mNCimW1RKKldOScSEvhRssicTqwVZ30wIDIG-9c5kYybGyb7Owu0Pnjg1H4Dq01RHWi7P6d5F7Kthcnf8I6MG4YPGQ8yuw_JdWNL1luywJoSuSsSMidAn130XOvi8aWtBZbA"}
    $secretId = "AKIDdylk4jikn-dUTdNuVOFWMrAE-K399KuR4IueRWlotS3_7VrS-r9BsjxJhqtm1Hm1"; //替换为用户的 secretId，请登录访问管理控制台进行查看和管理，https://console.cloud.tencent.com/cam/capi
    $secretKey = "vOxXz1n6GHLLLgnxjLp3FzbPmtMpfMl1OKBAYGW/rM0="; //替换为用户的 secretKey，请登录访问管理控制台进行查看和管理，https://console.cloud.tencent.com/cam/capi
    $region = "ap-shanghai"; //云托管写死上海
    $token = 'ZG57naPqVqKPW3W1ybc9huCUSbcB4cta8bbbb1aff1bf2fb6adacc0326f55f334s7m-o8ENv_uVVxbH99tRo0oJErbEI5wxm_OMtuHH_YYd7GnIWZR4BPuJEUpyUT_JWAWZYwW9L0-jzVtheHe0SqeCy0DPYRm1hR-E0HoXZIGuE8LU_HZJOH7yp0pmb2S5y-QCYlAaDs1fk3H1jbB-B__sY-jGPqz7leed8A5OwHO1mfbO36UOz-rbTHJdcWOvH4ygip7t4Zzp53XQPmLshX97yMpm8TR9_WB0lRk86dndLPx-Y9RbMT_vBQERq0zQS2xsS_pE-NnEkOo-VHhSHTd_OShU6rhNGxnh4aWHuYeXots8_kK2kSPq1HrTgaD4-ZIREwqyeh9-UBYUy5mNCimW1RKKldOScSEvhRssicTqwVZ30wIDIG-9c5kYybGyb7Owu0Pnjg1H4Dq01RHWi7P6d5F7Kthcnf8I6MG4YPGQ8yuw_JdWNL1luywJoSuSsSMidAn130XOvi8aWtBZbA';
    $cosClient = new Qcloud\Cos\Client(
        array(
            'region' => $region,
            'schema' => 'https', //协议头部，默认为http
            'credentials'=> array(
                'token'=> $token,
                'secretId'  => $secretId ,
                'secretKey' => $secretKey)));
    try {
        $local_path = __DIR__.'/composer.json';
        $result = $cosClient->PutObject(array(
            'Bucket' => $bucket, //存储桶名称，由BucketName-Appid 组成，可以在COS控制台查看 https://console.cloud.tencent.com/cos5/bucket
            'Key' => 'exampleobject',
            'Position' => 0, //追加对象位置
            'Body' => fopen($local_path, 'rb'),//读取文件内容
        ));
        // 请求成功
        print_r($result);

        $result2 = $cosClient->getObject(array(
            'Bucket' => $bucket, //存储桶名称，由BucketName-Appid 组成，可以在COS控制台查看 https://console.cloud.tencent.com/cos5/bucket
            'Key' => 'exampleobject',
            'SaveAs' => __DIR__.'/'.uniqid().'.json',
            /*
            'Range' => 'bytes=0-10',
            'ResponseCacheControl' => 'string',
            'ResponseContentDisposition' => 'string',
            'ResponseContentEncoding' => 'string',
            'ResponseContentLanguage' => 'string',
            'ResponseContentType' => 'string',
            'ResponseExpires' => 'string',
            */
        ));
        // 请求成功
        print_r($result2);
    } catch (\Exception $e) {
        // 请求失败
        echo($e);
    }
}

/**
 * GuzzleHttp\Command\Result Object
(
[ETag] => "c4fca7b15b937d4d8f932a55ff137c35"
[Position] => 100
[RequestId] => NjI0NmMwNWRfZDIxMDc1MDlfMTQ0NTFfMTU2NmY1Mw==
[Key] => exampleobject
[Bucket] => 7765-weiqing-0gk5cx1z2402c6fc-1310251736
[Location] => 7765-weiqing-0gk5cx1z2402c6fc-1310251736.cos.ap-shanghai.myqcloud.com/exampleobject
)

 */

